;(function () {
  new Vue({
    el: '#createBill',
    data: {
      billTypeList: [
        {name: '合同', code: 'HT'},
        {name: '退租', code: 'TZ'}
      ],
      billType: 'HT',
      code: '',
      beginDate: '',
      endDate: '',
      receivablesDate: '',
      expenditureList: [],
      regExpenditureList: [],
      flag: false,
      ktflag: true,
      customerName: '',
      contractName: '',
      tips: '请输入C开头13位管理编号',
      billFlag: false,
			relationFlag: false,
			contractBillName: '',
			customerNameKey: '',
			customerArr: [],
			customerActive: '',
			contractBillArr: [],
      ops: { bar: {background: '#c1c1c1'}},
      flowtype:"",
    },
    computed: {
      total () {
        let result = 0;
        this.expenditureList.map((item, index) => {
          result += Number(item.expense_amount)
        })
        this.regExpenditureList.map(item => {
          result -= Number(item.expense_amount)
        })
        return result
      }
    },
    filters: {
      abs (n) {
        return Math.abs(n)
      },
      formatNum: function (n) {
        return hlNumberUtil.formatNum(n)
      },
			timestamp: function(time) {
			    return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
			},
			contractState (str) {
				let res;
				switch (str){
					case 'PA':
					res = '审批通过'
						break;
					case 'EX':
					res = '执行中'
						break;
					case 'FN':
					res = '已到期'
						break;
					case 'EN':
					res = '已结束'
						break;
					default:
					res = '--'
						break;
				}
				return res
			},
			billState (str) {
				let res;
				switch (str){
					case 'SCP':
					res = '财务审核通过'
						break;
					case 'WJS':
					res = '待结算'
						break;
					case 'FN':
					res = '已完成'
						break;
					default:
					res = '--'
						break;
				}
				return res
			}
    },
    methods: {
      addexpenditure () {
        this.flag = true
        this.ktflag = true
        this.flowtype = 'RC'
      },
      regexpenditure () {
        this.flag = true
        this.ktflag = false
        this.flowtype = 'RF'
      },
      //切换账单类型
      billTypeCheck (code) {
        this.billType = code
        if (code == 'HT') {
          this.tips = '请输入C开头13位管理编号'
        }
        if (code == 'TZ') {
          this.tips = '请输入TZ开头14位管理编号'
        }
        this.customerName = ''
        this.code = ''
      },
      queryExpenditure (ops) {

        this.flag = false
        if (ops.name) {
          if (this.ktflag) {
            this.expenditureList.map((item, index) => {
              if (item.expense_name == ops.name) {
                this.expenditureList.splice(index, 1)
              }
            })
            this.expenditureList.push({
              expense_name: ops.name,
              expense_amount: ops.amount,
              expense_memo: ops.memo,
              tax_percent: ops.rate || '#',
              late_fee_precent: ops.percentage,
              expense_type: ops.type,
              expense_flow_type: this.flowtype,
              expenditure_id:ops.expenditureId
            })
          }
          if (!this.ktflag) {
            this.regExpenditureList.map((item, index) => {
              if (item.expense_name == ops.name) {
                this.expenditureList.splice(index, 1)
              }
            })
            this.regExpenditureList.push({
              expense_name: ops.name,
              expense_amount: ops.amount,
              expense_memo: ops.memo,
              tax_percent: ops.rate || '#',
              late_fee_precent: ops.percentage,
              expense_type: ops.type,
              expense_flow_type: this.flowtype,
              expenditure_id:ops.expenditureId
            })
          }

        }

      },
      timeValiData () {
        if (!this.beginDate) {
          setTimeout(() => {
            this.endDate = ''
          }, 200)
          hl.toast('请先选择开始时间')
          return
        }
        if (hlDateUtil.shellDate(this.beginDate) > hlDateUtil.shellDate(this.endDate)) {
          setTimeout(() => {
            this.endDate = ''
          }, 200)
          hl.toast('结束时间不能小于开始时间')
          return
        }
      },
      codeValidata () {
        if (!this.code) {
          hl.toast('管理编号不能为空')
          return
        }
        hlhttp.get({
          url: 'bill/search_cus_name_by_code',
          data: {bill_type: this.billType, code: this.code},
          success: (res) => {
            if (!res.customerName) {
              if (this.billType == 'HT') {
                hl.toast('合同管理编号不存在')
              }
              if (this.billType == 'TZ') {
                hl.toast('退租单号不存在')
              }
              this.customerName = ''
              this.contractName = ''
              return
            }
            this.customerName = res.customerName
            this.contractName = res.contractName
          }
        })
      },
      validata () {
        if (!this.code) {
          hl.toast('管理编号不能为空,请输入')
          return false
        }
        if (!this.beginDate) {
          hl.toast('请选择开始时间')
          return false
        }
        if (!this.endDate) {
          hl.toast('请选择结束时间')
          return false
        }
        if (!this.receivablesDate) {
          hl.toast('请选择收款截止时间')
          return false
        }
        let flag = false;
        if (this.expenditureList.length) {
          flag = true
        }
        if (this.regExpenditureList.length) {
          flag = true
        }
        if (!flag) {
          hl.toast('请收取一笔费项')
          return false
        }
        return true
      },
      shouBill () {
        if (!this.validata()) return;
        this.billFlag = true
      },
      saveSubmit () {
        this.expenditureList.map(item =>{
          item.tax_percent = item.tax_percent?item.tax_percent/100 : '#'
        })
        this.regExpenditureList.map(item =>{
          item.tax_percent = item.tax_percent?item.tax_percent/100 : '#'
        })
        let data = {
          bill_type: this.billType,
          code: this.code,
          period_begin: this.beginDate,
          period_end: this.endDate,
          payment_deadline_time: this.receivablesDate,
          receivable_expense: this.expenditureList,
          retreated_expense: this.regExpenditureList
        }
        hlhttp.post({
          url: 'bill/manual_create_bill',
          contentType: 'application/json;charset=utf-8',
          data: JSON.stringify(data),
          success: (res) => {
            hlUrlUtil.goTo('billDetail',{page: 'businessList', type:'new',ddtab: true, 'id': res.billId,ddtab:true})
          }
        })

      },
      del01 (i) {
        this.expenditureList.splice(i, 1)
      },
      del02 (i) {
        this.regExpenditureList.splice(i, 1)
      },
			//显示关联
			showRelation () {
				this.relationFlag = true;
				this.searchCustomer()
			},
			//关闭关联
			hideRelation () {
				this.relationFlag = false;
				this.resetButton()
			},
			//重置
			resetButton () {
				this.customerNameKey = '';
				this.customerArr = [];
				this.contractBillArr = [];
				this.customerActive = '';
				this.searchCustomer()
			},
			//搜索客户
			searchCustomer () {
				hlhttp.get({
					url: 'customer_manager_search',
					data: {
						keyword: this.customerNameKey || '#',
						customer_type: '#',
						industry_code: '#',
						rperson_id: '#',
						channel_category_code: '#',
						channel_name_code: '#',
						customer_state: '#',
						black_status: '#',
						limit: 10000,
						page: 1,
						columns: '#',
						orders: 'ASC'
					},
					success: (data) => {
						this.customerArr = data.myCustomers
					}
				})
			},
			//搜索all
			searchBillContract (item) {
				this.customerActive = item.id;
				this.customerName = item.customerName
				this.contractBillArr = [];
				if (this.billType == 'HT') {
					this.searchContract()
				}
				if (this.billType == 'TZ') {
					this.searchBill()
				}
			},
			//搜索合同
			searchContract () {
				hlhttp.get({
					url: 'zlf/contracts',
					data: {zlf_id: this.customerActive},
					success: (data) => {
						let arr = ['PA', 'EX', 'FN', 'EN']
						data.map((item) => {
							if (arr.indexOf(item.auditState) > -1) {
								this.contractBillArr.push(item)
							}
						})
					}
				})
			},
			//搜索退租
			searchBill () {
				hlhttp.get({
					url: 'customer_withdraws',
					data: {customer_id: this.customerActive},
					success: (data) => {
						let arr = ['SCP', 'WJS', 'FN']
						data.map((item) => {
							if (arr.indexOf(item.auditState) > -1) {
								this.contractBillArr.push(item)
							}
						})
					}
				})
			},
			//获取code
			codeSet (code) {
				this.code = code;
				this.relationFlag = false
			}
		},
    created () {
      if (document.body) {
　　　　document.body.scrollTop = 0
  　　 }
  　　if (document.documentElement) {
  　　　document.documentElement.scrollTop = 0
  　　}
      if (hlUrlUtil.GetQueryString('code')) {
        this.billType = hlUrlUtil.GetQueryString('type')
        this.code = hlUrlUtil.GetQueryString('code')
        this.customerName = unescape(hlUrlUtil.GetQueryString('name'))
        this.contractName = unescape(hlUrlUtil.GetQueryString('HTname'))
        if (this.billType == 'HT') {
          this.tips = '请输入C开头13位管理编号'
        }
        if (this.billType == 'TZ') {
          this.tips = '请输入TZ开头14位管理编号'
        }
      }
    }
  })
} ())
